package cn.zyl.demo.common.tools;

import cn.zyl.demo.common.enums.CharsetEnum;
import cn.zyl.demo.common.enums.CypherNameEnum;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/** CypherSha1Tool工具类说明:SHA-1工具类
 * @author wsz
 * @date 2020-07-28
 * @version v1.0
 */
public class CypherShaTool {
    private CypherShaTool(){throw new IllegalAccessError("CypherShaTool");}

    /**
     * 将byte转为16进制
     * @param bytes 字节
     * @return 字符串
     */
    private static String byte2Hex(byte[] bytes){
        StringBuilder sb = new StringBuilder();
        String temp;
        for (byte aByte : bytes) {
            temp = Integer.toHexString(aByte & 0xFF);
            //得到一位的进行补0操作
            if (temp.length() == 1) { sb.append("0"); }
            sb.append(temp);
        }
        return sb.toString();
    }
    public static String getSha1(String str) throws NoSuchAlgorithmException {
        MessageDigest mDigest = MessageDigest.getInstance(CypherNameEnum.SHA_1.getName());
        //byte[] result = mDigest.digest(input);
        mDigest.update(str.getBytes(CharsetEnum.UTF_8.getType()));
        byte[] result = mDigest.digest();
        StringBuilder sb = new StringBuilder();
        for (byte b : result) {
            sb.append(Integer.toString((b & 0xff) + 0x100, 16).substring(1));
        }
        return sb.toString();
    }
    /**
     * SHA1加密
     * @param str
     * @return
     */
    public static String string2Sha1(String str){
        if(str==null||str.length()==0){ return null; }
        char[] hexDigits = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
        try {
            MessageDigest mdTemp = MessageDigest.getInstance(CypherNameEnum.SHA_1.getName());
            mdTemp.update(str.getBytes(CharsetEnum.UTF_8.getType()));
            byte[] md = mdTemp.digest();

            int j = md.length;
            char[] buf = new char[j*2];
            int k = 0;
            for (byte byte0 : md) {
                buf[k++] = hexDigits[byte0 >>> 4 & 0xf];
                buf[k++] = hexDigits[byte0 & 0xf];
            }
            return new String(buf);
        } catch (Exception e) {
            //log.error("e",e);
            return null;
        }
    }

    /**
     *  利用Apache的工具类实现SHA-256加密
     *  所需jar包下載 http://pan.baidu.com/s/1nuKxYGh
     * @param str 加密后的报文
     * @return 密文
     */
    public static String string2SHA256(String str){
        MessageDigest messageDigest;
        String encodeStr = "";
        try {
            messageDigest = MessageDigest.getInstance(CypherNameEnum.SHA_256.getName());
            byte[] hash = messageDigest.digest(str.getBytes(CharsetEnum.UTF_8.getType()));

            encodeStr = HexTool.byteArr2HexStr(hash);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return encodeStr;
    }
    /**
     * 利用java原生的摘要实现SHA256加密
     * @param str 加密后的报文
     * @return 密文
     */
    public static String string2SHA256StrJava(String str){
        MessageDigest messageDigest;
        String encodeStr = "";
        try {
            messageDigest = MessageDigest.getInstance(CypherNameEnum.SHA_256.getName());
            messageDigest.update(str.getBytes(CharsetEnum.UTF_8.getType()));
            encodeStr = byte2Hex(messageDigest.digest());
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return encodeStr;
    }

    public static void main(String[] args) {
        try{
            String pass = "123456";
            System.out.println(pass);
            System.out.println(getSha1(pass));
            System.out.println(string2Sha1(pass));
            System.out.println(string2SHA256(pass));
            System.out.println(string2SHA256StrJava(pass));
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}